package cn.itsource.pethome.org.service.impl;

import cn.itsource.pethome.Basic.service.Impl.BaseServiceImpl;
import cn.itsource.pethome.org.constant.ShopConstant;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.mapper.EmployeeMapper;
import cn.itsource.pethome.org.mapper.ShopMapper;
import cn.itsource.pethome.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商家入驻相关service实现类
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 商家入驻
     * @param shop
     */
    @Override
    @Transactional
    public void settledln(Shop shop) {
        //获取用户
        Employee admin = shop.getAdmin();
        //设置用户状态
        admin.setState(ShopConstant.AUDIT);
        //将用户存入数据库
        employeeMapper.add(admin);
        //将用户数据绑定到shop，获取admin_id
        shop.setAdmin(admin);
        //将数据加入数据库
        shopMapper.add(shop);
    }

    @Override
    public Map<String, Object> findById(Long shop_id) {
       Map<String,Object> map = new HashMap<>();
       List<Shop> shop = shopMapper.findById(shop_id);
       map.put("shop",shop);
       return map;
    }

    @Override
    public Shop findByEmployee(Employee loginUser) {
        return shopMapper.findByEmployeeId(loginUser.getId());
    }
}
